package ferp.core.ai.tree2;

/**
 * User: igorgok
 * Date: 1/19/12 2:59 PM
 */
public class Score
{
  // flag mask
  public static final short FLAGS = 0x7000;
  // score mask
  public static final short SCORE = 0x0FFF;

  // flag - the sub-ree is complete
  public static final short COMPLETE = 0x1000;
  // this node was destroyed
  public static final short DESTROYED = 0x2000;

  // set individual player score in packed value
  public static short set(short packed, int player, int score)
  {
    int shift = player << 2;
    return (short)((packed | (0xF << shift)) & (score << shift));
  }
  // get individual player score from a packed value
  public static byte get(short packed, int player) {return (byte)((packed >> (player << 2)) & 0x0F);}
  // increment individual player score
  public static short increment(short packed, int player) {return (short)(packed + (1 << (player << 2)));}
  // decrement individual player score
  public static short decrement(short packed, int player) {return (short)(packed - (1 << (player << 2)));}
  // sum
  public static int sum(short packed) {return get(packed, 0) + get(packed, 1) + get(packed, 2);}
  // check if the packed score was propagated
  public static boolean isComplete(short packed) {return (packed & COMPLETE) != 0;}

  // convert packed score to string
  public static String toString(short packed)
  {
    return packed == 0 ? "---" : "" + hex(packed, 0) + hex(packed, 1) + hex(packed, 2) + (isComplete(packed) ? "'" : "");
  }

  private static char[] s2x = "0123456789ABCDEF".toCharArray();
  private static char hex(short packed, int player) {return s2x[get(packed, player)];}
}
